Vue.filter("filterTimes", function (val) {
    var y = new Date(val).getFullYear();
    var m = new Date(val).getMonth() + 1 >= 10 ? new Date(val).getMonth() + 1 : '0' + (new Date(val).getMonth() + 1);
    var d = new Date(val).getDate() >= 10 ? new Date(val).getDate() : '0' + new Date(val).getDate();
    return y + "-" + m + "-" + d;
})
Vue.filter("filterTimes1", function (val) {
    var y = new Date(val).getFullYear();
    var m = new Date(val).getMonth() + 1 >= 10 ? new Date(val).getMonth() + 1 : '0' + (new Date(val).getMonth() + 1);
    var d = new Date(val).getDate() >= 10 ? new Date(val).getDate() : '0' + new Date(val).getDate();
    var h = new Date(val).getHours() >= 10 ? new Date(val).getHours() : '0' + new Date(val).getHours();
    var ms = new Date(val).getMinutes() >= 10 ? new Date(val).getMinutes() : '0' + new Date(val).getMinutes();
    return y + "-" + m + "-" + d + " " + h + ":" + ms;
})
Vue.filter('filterTypes', function (val) {
    var type;
    var vals = Number(val);
    switch (vals) {
        case 1:
            type = '待确定'
            break;
        case 2:
            type = '已确认'
            break;
        case 3:
            type = '已开票'
            break;
        case 4:
            type = '已结算'
            break;
        default:
            type = '已撤销'
            break;
    }
    return type;
})
var app = new Vue({
    el: '#app',
    data: {
        billId: '',
        //getIp: 'http://192.168.0.180:8085/CARSERV',
        //getIp: 'http://carnt.carnettong.com:8088/CARSERV', //正式环境ip
        getIp: 'http://121.40.78.200:88/CARSERV',
        countDetail: '',
        tickTime: '', //开票时间
        settleTime:'',//结算时间
        settleRemark: '', //结算备注
        ticketNumber: '', //发票号码
        dialogVisible: false, //是否显示文字提示
        orderData: [], //订单数据
        once:true,//防止重复提交
    },
    created() {
        this.billId = location.search.split('=')[1];
        var obj = {};
        obj.BILLID = this.billId;
        this.getCountDetail(obj);
    },
    computed: {
        //对账状态(1、待确认 2、已确认、3、已开票  4、已结算)
        status() {
            if (this.countDetail.BILLSTATUS == 1) {
                return 1;
            } else if (this.countDetail.BILLSTATUS == 2) {
                return 2;
            } else if (this.countDetail.BILLSTATUS == 3) {
                return 4;
            } else if (this.countDetail.BILLSTATUS == 4) {
                return 5;
            }
        },
        //合计金额
        sum1: function () {
            let sum1 = 0;
            for (let i = 0; i < this.orderData.length; i++) {
                sum1 += parseFloat(this.orderData[i].TOTALFEE ? this.orderData[i].TOTALFEE : 0);
            }
            return sum1;
        },
        //实付金额
        sum2: function () {
            let sum2 = 0;
            for (let i = 0; i < this.orderData.length; i++) {
                sum2 += parseFloat(this.orderData[i].PAYFEE ? this.orderData[i].PAYFEE : 0);
            }
            return sum2;
        },
        //平台补贴
        sum3: function () {
            let sum3 = 0;
            for (let i = 0; i < this.orderData.length; i++) {
                sum3 += parseFloat(this.orderData[i].CARNTDISCOUNT ? this.orderData[i].CARNTDISCOUNT : 0);
            }
            return sum3;
        },
        //商家补贴
        sum4: function () {
            let sum4 = 0;
            for (let i = 0; i < this.orderData.length; i++) {
                sum4 += parseFloat(this.orderData[i].PLATFORMDISCOUNT ? this.orderData[i].PLATFORMDISCOUNT : 0);
            }
            return sum4;
        },
        //结算价 = (实付金额+平台补贴)*结算费率-商家补贴  可能为负值
        sum5: function () {
            let sum5 = 0;
            sum5 = (this.sum2 + this.sum3) * this.countDetail.RATIO - this.sum4;
            sum5 = sum5.toFixed(2);
            if(isNaN(sum5)){
                return 0;
            }else{
                return sum5;
            }
        }
    },
    methods: {
        //时间格式化
        format: function (val) {
            var y = new Date(val).getFullYear();
            var m = new Date(val).getMonth() + 1 >= 10 ? new Date(val).getMonth() + 1 : '0' + (new Date(val).getMonth() + 1);
            var d = new Date(val).getDate() >= 10 ? new Date(val).getDate() : '0' + new Date(val).getDate();
            var h = new Date(val).getHours() >= 10 ? new Date(val).getHours() : '0' + new Date(val).getHours();
            var ms = new Date(val).getMinutes() >= 10 ? new Date(val).getMinutes() : '0' + new Date(val).getMinutes();
            return y + "-" + m + "-" + d + " " + h + ":" + ms;
        },
        getCountDetail(obj) {
            this.$http.get(this.getIp + '/PlatformBill/getBillDetail.action', {params: obj}).then(
                    res => {
                        if (res.body.STATUC_CODE == 1) {
                            this.countDetail = res.body;
                            this.ticketNumber = this.countDetail.TICKETNO;
                            this.tickTime = this.countDetail.TICKETDATE;
                            this.settleRemark = this.countDetail.BILLREMARK;
                            this.orderData = res.body.ORDERARR;
                            //this.settleTime = this.countDetail.SETTLEMENTDATE;
                        }
                    }, err => {
                console.log(err);
            }
            );
        },
        //保存备注
        eidtRemark() {
            if(this.settleRemark == '' || this.settleRemark == null){
                this.$alert('请填写备注!', '提示');
                return;
            }
            let obj = {};
            obj.BILLID = this.billId;
            obj.REMARK = this.settleRemark ? this.settleRemark : '';
            obj.TICKETDATE = this.tickTime ? this.format(this.tickTime) : '';
            obj.TICKETNO = this.ticketNumber ? this.ticketNumber : '';
            obj.SETTLEMENTDATE = this.countDetail.SETTLEMENTDATE;
            obj.BILLSTATUS = this.countDetail.BILLSTATUS;
            if(this.once){
                this.once = false;
                this.$http.post(this.getIp + '/PlatformBill/editPlatformBill.action', obj, {emulateJSON: true}).then(
                        res => {
                            if (res.body.STATUC_CODE == 1) {
                                this.$alert('保存备注成功!', '提示',{
                                    showClose:false,
                                    showConfirmButton:false
                                });
                                setTimeout(function(){location.href="./workOrder.html"},1000);
                            }
                        }, err => {
                            console.log(err);
                    }
                );
            }
        },
        //收票确认
        comfirmTick() {
            if(this.countDetail.BILLSTATUS == 1){
                this.$alert('已确定才能收票确认!', '提示');
                return;
            }else if(this.countDetail.BILLSTATUS > 2){
                this.$alert('已收票确认!', '提示');
                return;
            }
            //已确定才能点
            if (!this.tickTime) {
                this.$alert('请填写开票时间!', '提示');
                return;
            }
            if (!this.ticketNumber) {
                this.$alert('请填写发票号码!', '提示');
                return;
            }
            let obj = {};
            obj.BILLID = this.billId;
            obj.REMARK = this.settleRemark ? this.settleRemark : '';
            obj.TICKETDATE = this.tickTime ? this.format(this.tickTime) : '';
            obj.TICKETNO = this.ticketNumber ? this.ticketNumber : '';
            obj.SETTLEMENTDATE = this.countDetail.SETTLEMENTDATE;
            obj.BILLSTATUS = 3;
            if(this.once){
                this.once = false;
                this.$http.post(this.getIp + '/PlatformBill/editPlatformBill.action', obj, {emulateJSON: true}).then(
                        res => {
                            if (res.body.STATUC_CODE == 1) {
                                this.$alert('收票确认成功!', '提示',{
                                    showClose:false,
                                    showConfirmButton:false
                                });
                                setTimeout(function(){location.href="./workOrder.html"},1000);
                            }
                        }, err => {
                    console.log(err);
                    }
                );
            }
        },
        //撤销对账单
        cacleOrder() {
            let obj = {};
            obj.BILLID = this.billId;
            obj.REMARK = this.settleRemark ? this.settleRemark : '';
            obj.TICKETDATE = this.tickTime ? this.format(this.tickTime) : '';
            obj.TICKETNO = this.ticketNumber ? this.ticketNumber : '';
            obj.SETTLEMENTDATE = this.countDetail.SETTLEMENTDATE;
            obj.BILLSTATUS = 0;
            if(this.once){
                this.once = false;
                this.$http.post(this.getIp + '/PlatformBill/editPlatformBill.action', obj, {emulateJSON: true}).then(
                        res => {
                            if (res.body.STATUC_CODE == 1) {
                                this.$alert('撤销账单成功!', '提示',{
                                    showClose:false,
                                    showConfirmButton:false
                                });
                                setTimeout(function(){location.href="./workOrder.html"},1000);
                            }
                        }, err => {
                    console.log(err);
                    }
                );
            }
        },
        //导出
        excel() {
            let str1 = 'BILLID=' + this.billId;
            let url = this.getIp + '/PlatformBill/getBillDetailExcel.action?' + str1;
            window.open(url);
        },
    },
    filters:{
        //小数换算百分数
        percentage(val){
            let b = (val * 100).toFixed(2) + "%";
            if(val.match('%')){
                return val;
            }else{
                return b;
            }
        },
    },
    components: {
    }
})
